<template>
  <div class="statistics-container">
    <h2 class="section-title">数据统计</h2>
    
    <div class="chart-section">
      <!-- 各实验台制冷剂回收率趋势 -->
      <el-row :gutter="20">
        <el-col :span="24">
          <div class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">各实验台制冷剂回收率趋势</h3>
              <div class="chart-filter">
                <el-date-picker
                  v-model="trendMonth"
                  type="month"
                  placeholder="选择月份"
                  size="small"
                  style="width: 140px; margin-right: 10px;"
                  @change="handleMonthChange"
                ></el-date-picker>
                <el-select 
                  v-model="trendLab" 
                  placeholder="选择实验台" 
                  size="small"
                  style="width: 140px; margin-right: 10px;"
                >
                  <el-option label="全部实验台" value="all"></el-option>
                  <el-option label="A01实验台" value="A01"></el-option>
                  <el-option label="A02实验台" value="A02"></el-option>
                </el-select>
                <el-button 
                  type="primary" 
                  size="small" 
                  @click="loadTrendData"
                >查询</el-button>
              </div>
            </div>
            <div class="chart-container" v-if="hasTrendData" ref="trendChart" style="height: 400px;"></div>
            <div class="chart-placeholder" v-else>
              <i class="el-icon-data-line"></i>
              <p>请查询以显示图表</p>
            </div>
          </div>
        </el-col>
      </el-row>
      
      <!-- 每种制冷剂的回收率 -->
      <el-row :gutter="20">
        <el-col :span="24">
          <div class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">每种制冷剂的回收率</h3>
              <div class="chart-filter">
                <el-date-picker
                  v-model="refrigerantMonth"
                  type="month"
                  placeholder="选择月份"
                  size="small"
                  style="width: 140px; margin-right: 10px;"
                  @change="handleRefrigerantMonthChange"
                ></el-date-picker>
                <el-button 
                  type="primary" 
                  size="small" 
                  @click="loadRefrigerantData"
                >查询</el-button>
              </div>
            </div>
            <div class="chart-container" v-if="hasRefrigerantData" ref="refrigerantChart" style="height: 400px;"></div>
            <div class="chart-placeholder" v-else>
              <i class="el-icon-pie-chart"></i>
              <p>请查询以显示图表</p>
            </div>
          </div>
        </el-col>
      </el-row>
      
      <!-- 实验台工作时间 -->
      <el-row :gutter="20">
        <el-col :span="24">
          <div class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">实验台工作时间</h3>
              <div class="chart-filter">
                <el-date-picker
                  v-model="workHoursMonth"
                  type="month"
                  placeholder="选择月份"
                  size="small"
                  style="width: 140px; margin-right: 10px;"
                  @change="handleWorkHoursMonthChange"
                ></el-date-picker>
                <el-button 
                  type="primary" 
                  size="small" 
                  @click="loadWorkHoursData"
                >查询</el-button>
              </div>
            </div>
            
            <!-- 查询前显示占位提示，查询后显示表格 -->
            <div v-if="!hasWorkHoursData" class="table-placeholder">
              <i class="el-icon-document"></i>
              <p>请查询以显示表格</p>
            </div>
            
            <div v-if="hasWorkHoursData" class="table-wrapper">
              <div class="table-title">
                {{ formattedWorkHoursMonth }}实验台工作时间
              </div>
              <div class="table-content">
                <el-table 
                  :data="workHoursData" 
                  stripe 
                  border
                  v-loading="workHoursLoading"
                  style="width: 100%"
                >
                  <el-table-column prop="laboratory" label="实验室" width="120" align="center"></el-table-column>
                  <el-table-column prop="workbench" label="实验台" width="120" align="center"></el-table-column>
                  <el-table-column prop="workHours" label="工作时间(小时)" width="140" align="center"></el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'DataStatistics',
  data() {
    return {
      trendMonth: '',
      trendLab: 'all',
      refrigerantMonth: '',
      workHoursMonth: '',
      trendChart: null,
      refrigerantChart: null,
      formattedMonth: '',
      formattedRefrigerantMonth: '',
      formattedWorkHoursMonth: '',
      hasTrendData: false,
      hasRefrigerantData: false,
      hasWorkHoursData: false,
      workHoursData: [
        { laboratory: '实验室1', workbench: 'A01', workHours: 120 },
        { laboratory: '实验室1', workbench: 'A02', workHours: 95 },
        { laboratory: '实验室2', workbench: 'B01', workHours: 110 },
        { laboratory: '实验室2', workbench: 'B02', workHours: 85 },
      ],
      workHoursLoading: false
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    if (this.trendChart) {
      this.trendChart.dispose();
    }
    if (this.refrigerantChart) {
      this.refrigerantChart.dispose();
    }
  },
  methods: {
    initTrendChart() {
      this.trendChart = echarts.init(this.$refs.trendChart);
      this.updateTrendChart();
    },
    initRefrigerantChart() {
      this.refrigerantChart = echarts.init(this.$refs.refrigerantChart);
      this.updateRefrigerantChart();
    },
    handleMonthChange(val) {
      if (val) {
        const date = new Date(val);
        this.formattedMonth = `${date.getFullYear()}年${(date.getMonth() + 1).toString().padStart(2, '0')}月`;
      } else {
        this.formattedMonth = '';
      }
    },
    handleRefrigerantMonthChange(val) {
      if (val) {
        const date = new Date(val);
        this.formattedRefrigerantMonth = `${date.getFullYear()}年${(date.getMonth() + 1).toString().padStart(2, '0')}月`;
      } else {
        this.formattedRefrigerantMonth = '';
      }
    },
    handleWorkHoursMonthChange(val) {
      if (val) {
        const date = new Date(val);
        this.formattedWorkHoursMonth = `${date.getFullYear()}年${(date.getMonth() + 1).toString().padStart(2, '0')}月`;
      } else {
        this.formattedWorkHoursMonth = '';
      }
    },
    updateTrendChart() {
      const chartTitle = `${this.formattedMonth}${this.trendLab === 'all' ? '各实验台' : this.trendLab}制冷剂回收率趋势`;
      
      const xData = ['制冷剂1', '制冷剂2', '制冷剂3', '制冷剂4'];
      const seriesData = [
        {
          name: chartTitle,
          type: 'bar',
          data: [78, 80, 85, 90],
          itemStyle: {
            color: '#5470C6'
          },
          label: {
            show: true,
            position: 'top',
            formatter: '{c}%'
          }
        }
      ];

      const option = {
        title: {
          text: chartTitle,
          left: 'center',
          textStyle: {
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: '{b}<br/>{a}: {c}%'
        },
        legend: {
          data: [chartTitle],
          right: 10,
          top: 0
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLabel: {
            interval: 0,
            rotate: 0
          }
        },
        yAxis: {
          type: 'value',
          name: '回收率(%)',
          min: 50,
          max: 100,
          axisLine: {
            show: true
          },
          axisLabel: {
            formatter: '{value}%'
          }
        },
        series: seriesData
      };

      this.trendChart.setOption(option);
    },
    updateRefrigerantChart() {
      const chartTitle = `${this.formattedRefrigerantMonth}各种制冷剂的回收率`;
      
      const xData = ['制冷剂A', '制冷剂B', '制冷剂C', '制冷剂D'];
      const seriesData = [
        {
          name: chartTitle,
          type: 'bar',
          data: [65, 72, 88, 91],
          itemStyle: {
            color: '#FF9F00'
          },
          label: {
            show: true,
            position: 'top',
            formatter: '{c}%'
          }
        }
      ];

      const option = {
        title: {
          text: chartTitle,
          left: 'center',
          textStyle: {
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: '{b}<br/>{a}: {c}%'
        },
        legend: {
          data: [chartTitle],
          right: 10,
          top: 0
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: xData,
          axisLabel: {
            interval: 0,
            rotate: 0
          }
        },
        yAxis: {
          type: 'value',
          name: '回收率(%)',
          min: 50,
          max: 100,
          axisLine: {
            show: true
          },
          axisLabel: {
            formatter: '{value}%'
          }
        },
        series: seriesData
      };

      this.refrigerantChart.setOption(option);
    },
    handleResize() {
      if (this.trendChart) {
        this.trendChart.resize();
      }
      if (this.refrigerantChart) {
        this.refrigerantChart.resize();
      }
    },
    loadTrendData() {
      if (!this.trendMonth) {
        this.$message.warning('请选择月份');
        return;
      }
      
      this.$message.success(`加载趋势数据: ${this.formattedMonth}, ${this.trendLab}`);
      this.hasTrendData = true;
      
      this.$nextTick(() => {
        this.initTrendChart();
      });
    },
    loadRefrigerantData() {
      if (!this.refrigerantMonth) {
        this.$message.warning('请选择月份');
        return;
      }

      this.$message.success(`加载制冷剂数据: ${this.formattedRefrigerantMonth}`);
      this.hasRefrigerantData = true;

      this.$nextTick(() => {
        this.initRefrigerantChart();
      });
    },
    loadWorkHoursData() {
      if (!this.workHoursMonth) {
        this.$message.warning('请选择月份');
        return;
      }

      this.workHoursLoading = true;
      this.$message.success(`查询工作时间数据: ${this.formattedWorkHoursMonth}`);
      
      // 模拟API调用
      setTimeout(() => {
        this.workHoursLoading = false;
        this.hasWorkHoursData = true;
      }, 500);
    }
  }
}
</script>

<style scoped>
.statistics-container {
  padding: 20px;
}

.section-title {
  margin-bottom: 20px;
  color: #333;
  text-align: center;
}

.chart-section {
  margin-bottom: 20px;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.chart-filter {
  display: flex;
  align-items: center;
}

.chart-title {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}

.chart-card {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.chart-container {
  width: 100%;
  height: 400px;
}

.chart-placeholder, .table-placeholder {
  height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #999;
  background: #f9f9f9;
  border-radius: 4px;
}

.chart-placeholder i, .table-placeholder i {
  font-size: 36px;
  margin-bottom: 10px;
}

.table-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.table-title {
  font-size: 18px;
  font-weight: bold;
  margin: 15px 0;
  color: #333;
  text-align: center;
}

.table-content {
  width: 80% auto;
  margin: 0 auto;
}

.el-row {
  margin-bottom: 20px;
}
</style>